(function() {


    angular.module('BlurAdmin.pages.console')
        .controller('statisticsCtrl', statisticsCtrl);

    function statisticsCtrl($scope, tongxin, $http, $myconfig, $datepicker) {
        //饼状图数据
        $scope.tpo = 1;
        $scope.myTabs = function(val) {
            $scope.tpo = val;
        }
        $scope.dat = {
            dat1: $datepicker.dataNew(),
            dat2: $datepicker.dataNew(),
        };
        $scope.dat.dat1.date_ = '';
        $scope.dat.dat2.date_ = '';

        //金鼻光
        var jbg_data = {
            name: '理疗时间',
            datapoints: [
                { x: '美容', y: 0 },
                // { x: '经血', y: 0 },
                // { x: '通经络', y: 0 },
                // { x: '调血气', y: 0 }
            ]
        };
        $scope.jbg_data = [jbg_data];
        // $scope.jbg_legend_name = [];
        $scope.jbg_legend_name = ['1'];

        $scope.jbg_config = {
            title: {
                text: '金鼻光使用情况(分钟)',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} "
            },
            legend: {
                //  show: false,
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} "
                },
                orient: 'horizontal',
                y: 'bottom',
                padding: '20',
                data: $scope.jbg_legend_name,
                x: 'center'
            },
            series: [{
                name: '访问来源',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
            }]
        };
        var opt = {
            "dev_name": "",
            "start_time": "",
            "end_time": ""
        }


        //换颜宝
        var hyb_data = {
            name: '理疗时间',
            datapoints: [
                { x: '抗皱美白', y: 0 },
                // { x: '祛斑祛痘', y: 0 },
                // { x: '润肤嫩肤', y: 0 },
                // { x: '倒模导入', y: 0 }
            ]
        };

        $scope.hyb_data = [hyb_data];
        $scope.hyb_legend_name = [];
        $scope.hyb_config = {
            title: {
                text: '换颜宝使用情况(分钟)',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} "
            },
            legend: {
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} "
                },
                orient: 'horizontal',
                y: 'bottom',
                padding: '20',
                data: $scope.hyb_legend_name,
                x: 'center'
            },
            series: [{
                name: '访问来源',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
            }]
        };

        //请求数据
        function jbg_shebie() { //金鼻光
            var opt = {
                "dev_name": "金鼻光",
                "start_time": $scope.dat.dat1.date_,
                "end_time": $scope.dat.dat2.date_

            }
            $http.get($myconfig.api.Physicallogs_controls_mode, { params: opt }).then(function(res) {
                console.log(res);
                // { x: '暂无数据', y: 0 }
                var arr = [];
                for (var i = 0; i < res.data.data.length; i++) {
                    arr.push({});
                    arr[i].x = res.data.data[i].mode;
                    arr[i].y = ~~(res.data.data[i].durations / 60);
                    // $scope.jbg_legend_name.push(res.data.data[i].mode);
                    $scope.jbg_legend_name[i] = res.data.data[i].mode;
                    console.log(res.data.data[i].mode);

                }
                if (res.data.data.length == 0) {
                    $scope.is_jbg = false;
                    arr = [{ x: '暂无数据', y: 0 }];
                } else {
                    $scope.is_jbg = true;
                }
                jbg_data.datapoints = arr;
                $scope.jbg_data = [jbg_data];
            })
        }


        function hyb_shebie() { //焕颜宝
            var opt = {
                "dev_name": "焕颜宝",
                "start_time": $scope.dat.dat1.date_,
                "end_time": $scope.dat.dat2.date_
            }
            $http.get($myconfig.api.Physicallogs_controls_mode, { params: opt }).then(function(res) {
                var arr = [];
                var name = [];
                for (var i = 0; i < res.data.data.length; i++) {
                    arr.push({});
                    arr[i].x = res.data.data[i].mode;
                    arr[i].y = ~~(res.data.data[i].durations / 60);
                    // if (res.data.data.length < $scope.hyb_legend_name.length) {}
                    $scope.hyb_legend_name.push(res.data.data[i].mode);
                }
                $scope.hyb_legend_name.length = res.data.data.length;

                if (res.data.data.length == 0) {
                    arr = [{ x: '暂无数据', y: 100 }];
                    $scope.hyb_legend_name = ['暂无数据'];
                    $scope.is_hyb = false;

                } else {
                    $scope.is_hyb = true;
                }
                hyb_data.datapoints = arr;
                console.log(hyb_data.datapoints);

                $scope.hyb_data = [hyb_data];

            })
        }

        $scope.Search = function() {
            jbg_shebie();
            hyb_shebie();
        }
        $scope.Search();

    }
})();